package com.sgb.kjwl.viewmodel.enterprise;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.AttentionEntity;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.model.entity.enterprise.QuestionEnyiy;
import com.sgb.kjwl.model.entity.enterprise.ServiceProjectClassEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.activity.enterprise.ProfessionalQuoteDetailActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProfessionalQuoteDetailModel extends BaseViewModel {
    private ProfessionalQuoteDetailActivity professionalQuoteDetailActivity;
    public List<ServiceProjectClassEntity> serviceProList = new ArrayList<>();
    public List<Map> maps = new ArrayList<>();

    public ProfessionalQuoteDetailModel(ProfessionalQuoteDetailActivity professionalQuoteDetailActivity) {
        this.professionalQuoteDetailActivity = professionalQuoteDetailActivity;
    }

    public void getData(long id) {
        professionalQuoteDetailActivity.loading();
        DialogHelper.showProgressDialog(professionalQuoteDetailActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getOfferDetail(id).enqueue(new Callback<BaseEntity<EpQuoteEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<EpQuoteEntity>> call, Response<BaseEntity<EpQuoteEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(professionalQuoteDetailActivity, "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    professionalQuoteDetailActivity.showQuoteDetail(response.body().getData());
                    professionalQuoteDetailActivity.success();
                } else {

                    MToast.showToast(professionalQuoteDetailActivity, response.body().getMsg());
                    professionalQuoteDetailActivity.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<EpQuoteEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                professionalQuoteDetailActivity.error();
            }
        });


        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getquestionlistdata("6").enqueue(new Callback<BaseEntity<List<QuestionEnyiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<QuestionEnyiy>>> call, Response<BaseEntity<List<QuestionEnyiy>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {

                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    professionalQuoteDetailActivity.showQuestionListdata(response.body().getData());

                } else {
                    MToast.showToast(professionalQuoteDetailActivity, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<QuestionEnyiy>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();

            }
        });


    }


    public void getRecomendListdata(String id, String serviveid) {
        NewAPIManager.getInstance().getEnterPrisenewAPI().getRecommendlistdata(id + "", 1, 6, serviveid).enqueue(new Callback<BaseEntity<ListResponse<List<EpQuoteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Response<BaseEntity<ListResponse<List<EpQuoteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {

                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    professionalQuoteDetailActivity.showRecommend(response.body().getData().getList());

                } else {
                    MToast.showToast(professionalQuoteDetailActivity, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void focusService(Map<String, Object> map, int isGz) {
        DialogHelper.showProgressDialog(professionalQuoteDetailActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().saveAttention(map).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData().contains("下架")) {
                        MToast.showToast(professionalQuoteDetailActivity, response.body().getData());
                    } else {
                        professionalQuoteDetailActivity.setTvIsGz(isGz);
                        professionalQuoteDetailActivity.setDataId(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getAttentionStatus(String userNo, int type, String dataId) {
        DialogHelper.showProgressDialog(professionalQuoteDetailActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getPriceOption(userNo, type+"", dataId).enqueue(new Callback<BaseEntity<List<AttentionEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AttentionEntity>>> call, Response<BaseEntity<List<AttentionEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {//已关注
                        professionalQuoteDetailActivity.setTvIsGz(0);
                        professionalQuoteDetailActivity.setDataId(String.valueOf(response.body().getData().get(0).getId()));
                    } else {
                        professionalQuoteDetailActivity.setTvIsGz(1);
                    }

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AttentionEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public List<ServiceProjectClassEntity> serviceProToBean(String json) {
        serviceProList = new Gson().fromJson(json, new TypeToken<List<ServiceProjectClassEntity>>() {
        }.getType());
        return serviceProList;
    }

    public List<Map> servicePriceToBean(String json) {
        maps = new Gson().fromJson(json, new TypeToken<List<Map>>() {
        }.getType());
        return maps;
    }

}
